//-----------------------------------------------------------------------
// <copyright file="IDSSortDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class IDSSortDAOBase
    {
        #region Common methods
        public virtual IDSSort CreateIDSSortFromReader(IDataReader reader)
        {
            IDSSort item = new IDSSort();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("IDSSortID"))) item.IDSSortID = (int)reader["IDSSortID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) item.SortOrder = (int)reader["SortOrder"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDisplayed"))) item.IsDisplayed = (bool)reader["IsDisplayed"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateIDSSortFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateIDSSort methods
            
        public virtual void CreateIDSSort(IDSSort iDSSort)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spIDSSortsInsert");
                
                dbCommandWrapper.AddInParameter("@Name", DbType.String, iDSSort.Name);
                dbCommandWrapper.AddInParameter("@SortOrder", DbType.Int32, iDSSort.SortOrder);
                dbCommandWrapper.AddInParameter("@IsDisplayed", DbType.Boolean, iDSSort.IsDisplayed);
                dbCommandWrapper.AddOutParameter("@IDSSortID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                iDSSort.IDSSortID = (int)dbCommandWrapper.GetParameterValue("@IDSSortID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateIDSSortException, ex);
            }
        }

        #endregion

        #region UpdateIDSSort methods
        
        public virtual void UpdateIDSSort(IDSSort iDSSort)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spIDSSortsUpdate");            
                
                dbCommandWrapper.AddInParameter("@IDSSortID", DbType.Int32, iDSSort.IDSSortID);
                dbCommandWrapper.AddInParameter("@Name", DbType.String, iDSSort.Name);
                dbCommandWrapper.AddInParameter("@SortOrder", DbType.Int32, iDSSort.SortOrder);
                dbCommandWrapper.AddInParameter("@IsDisplayed", DbType.Boolean, iDSSort.IsDisplayed);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateIDSSortException, ex);
            }
        }
        
        #endregion

        #region DeleteIDSSort methods
        public virtual void DeleteIDSSort(int iDSSortID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spIDSSortsDelete");
                
                dbCommandWrapper.AddInParameter("@IDSSortID", DbType.Int32, iDSSortID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteIDSSortException, ex);
            }
        }

        #endregion

        #region GetIDSSort methods
        
        public virtual IDSSort GetIDSSort(int iDSSortID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spIDSSortsGet");
                
                dbCommandWrapper.AddInParameter("@IDSSortID", DbType.Int32, iDSSortID);
                
                IDSSort iDSSort =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        iDSSort = CreateIDSSortFromReader(reader);
                        reader.Close();
                    }
                }
                return iDSSort;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetIDSSortException, ex);
            }
        }
        
        #endregion

        #region GetIDSSorts methods
        public virtual IDSSortCollection GetIDSSorts(IDSSortColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spIDSSortsGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                IDSSortCollection iDSSortCollection = new IDSSortCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        IDSSort iDSSort = CreateIDSSortFromReader(reader);
                        iDSSortCollection.Add(iDSSort);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return iDSSortCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetIDSSortsException, ex);
            }
        }
        
        public virtual IDSSortCollection GetIDSSorts(IDSSortColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetIDSSorts(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}